package txtpack.Work;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

public class Exam03 {
    public static void main(String[] args) {
        //文件合并
        File target = new File("E:\\IO流练习\\OutPutStream\\new2");
        File dest = new File("E:\\IO流练习\\OutPutStream\\curent");
        mergeFile(target, dest);
    }

    private static void mergeFile(File target, File dest) {
        File file = new File(dest, "new1" + ".mp4");
        if (!dest.getParentFile().exists()) { // 判断父级目录是否存在不存在创建父级目录
            dest.getParentFile().mkdirs();
        }
        try (FileOutputStream out = new FileOutputStream(file,true)) {
            File[] files = target.listFiles();
            if (files != null) {
                Arrays.sort(files, new Comparator<File>() {
                    public int compare(File file1, File file2) {
                        int i1 = Integer.parseInt(file1.getName().split("\\.")[0]);
                        int i2 = Integer.parseInt(file2.getName().split("\\.")[0]);
                        return i1 - i2;
                    }
                });
                for (File file1 : files) {
                    try (FileInputStream in = new FileInputStream(file1)) {
                        byte[] buffer = in.readAllBytes();
                        out.write(buffer);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

